package com.jintian.smart.kernel.flowable.component.action.task;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.jintian.smart.kernel.core.flow.FlowAction;
import com.jintian.smart.kernel.core.flow.ProcessControl;
import com.jintian.smart.kernel.flowable.component.action.base.AbstractTaskAction;
import org.flowable.bpmn.model.FlowNode;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskInfo;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.stereotype.Component;

/**
 * 完成当前任务，包含委派任务处理
 */
@Component
public class WithdrawTaskAction extends AbstractTaskAction {
    @Override
    public FlowAction getAction() {
        return FlowAction.withdraw;
    }

    @Override
    protected List<TaskInfo> innerExecute(TaskEntity task, ProcessControl processControl) throws Exception {
        throw new UnsupportedOperationException("暂未实现取回任务");
    }

    @Override
    public List<? extends FlowNode> queryTaskNextActivityNode(Task task) {
        if (task.getTaskLocalVariables().get("prevTaskId") != null) {
            HistoricTaskInstance prevTask = this.historyService().createHistoricTaskInstanceQuery()
                    .taskId((String) task.getTaskLocalVariables().get("prevTaskId")).singleResult();
            org.flowable.bpmn.model.Process process = this.getMainProcess(task.getProcessDefinitionId());
            FlowNode node = (FlowNode) process.getFlowElement(prevTask.getTaskDefinitionKey());
            return new ArrayList<>(Arrays.asList(node));
        }
        return Collections.emptyList();
    }
}
